package com.shanghaionstar.nav;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.adapter.NavSearchItemGridViewAdapter;
import com.shanghaionstar.nav.iview.INavSearchMoreView;
import com.shanghaionstar.nav.model.NavInfoModel;
import com.shanghaionstar.nav.presenter.NavSearchMorePresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;
import java.util.Map;

/**
 * Created by 6335yz on 2016/7/7.
 */
public class NavSearchMoreFragment extends BaseFragment implements INavSearchMoreView {

    private NavSearchMorePresenter navSearchMorePresenter;
    @FindViewById(R.id.gridView_more_search_view)
    private GridView gridView_more_search_view;

    //GridView的图片和文案数据
    private AbstractMapActivity mapActivity;
    private FragmentManager fragmentManager;
    @FindViewById
    private TopLayout top_layout;

    @Override
    public int getLayoutId() {
        return R.layout.nav_more_search;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        navSearchMorePresenter = new NavSearchMorePresenter(mapActivity.getScheduler(), mapActivity);
        navSearchMorePresenter.setView(this);
        initGridViewData();
        initBack();
        setTitle();
    }

    private void setTitle() {
        Bundle bundle = getArguments();
        String defTitle = getResources().getString(R.string.nav_more_search);
        if (bundle != null && !TextUtils.isEmpty(bundle.getString(SEARCHMORE_TITLE))) {
            top_layout.setTopTitle(bundle.getString(SEARCHMORE_TITLE));
        } else {
            top_layout.setTopTitle(defTitle);
        }
//        ViewCompat.setTransitionName(top_layout.findViewById(R.id.top_title),mapActivity.getString(R.string.trans_nav_more));
    }

    private void initBack() {
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Categorysearchmore_return.getStringValue());
                goBack();
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mapActivity = (AbstractMapActivity) activity;
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    private String[] gridViewTextInfos;
    private int[] gridViewImages;

    /**
     * @Title: initGridViewData
     * @Description: 初始化Gridview的数据
     */
    public void initGridViewData() {
        TypedArray ar = getResources().obtainTypedArray(R.array.gridItem_image_more);
        int len = ar.length();
        gridViewImages = new int[len];
        for (int i = 0; i < len; i++) {
            gridViewImages[i] = ar.getResourceId(i, 0);
        }
        ar.recycle();
        gridViewTextInfos = getResources().getStringArray(R.array.more_griditem_info);
        navSearchMorePresenter.loadSearchItem(gridViewImages, gridViewTextInfos);
    }

    @Override
    public void showSearchItem(final List<Map<String, Object>> datas) {
        NavSearchItemGridViewAdapter adapter = new NavSearchItemGridViewAdapter(datas, mapActivity);
        gridView_more_search_view.setAdapter(adapter);
        gridView_more_search_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navSearchMorePresenter.onGridViewItemClick(position, gridViewTextInfos);
            }
        });
    }

    @Override
    public void setLoading(boolean isLoading) {
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showAlertDialog(mapActivity, getString(R.string.dialog_warning), e.getMessage());
    }

    @Override
    public void onReponseSuccess(NavInfoModel navInfoModel) {
    }

    @Override
    public Activity onActivity() {
        return null;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (navSearchMorePresenter != null) {
            navSearchMorePresenter.finish();
            navSearchMorePresenter = null;
        }
    }

    public static final String SEARCHMORE_TITLE = "searchMoreTilte";

    public static NavSearchMoreFragment newInstance(String title) {
        NavSearchMoreFragment navSearchMoreFragment = new NavSearchMoreFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SEARCHMORE_TITLE, title);
        navSearchMoreFragment.setArguments(bundle);
        return navSearchMoreFragment;
    }
}
